home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Turnbull China Bikeride
/
Turnbull China Bikeride - Disc 2.iso
/
BARNET
/
FREENET
/
BRODIE
/
INTERNET
/
!InternetD
/
c
/
chargen
< prev
next >
Wrap
Text File
|
1995-06-07
|
2KB
|
81 lines
#include "inetd.h"
#define CHARGENSIZE 76
void chargen_tcp(int s)
{
int t;
struct sockaddr_in address;
int size = sizeof(struct sockaddr_in);
inet_handler *ih;
t = accept(s, (struct sockaddr *)&address, &size);
if (t == -1) return;
ih = worker_register(t, chargen_data, worker_io_write);
if (ih) {
ih->data.chargen.state = ' ';
ih->data.chargen.offset = 0;
if (ioctl(t, FIONBIO, 1) == -1) {
syslog(0, "ioctl() failed for chargen/tcp");
}
}
else {
kill_socket(&t);
}
}
void chargen_udp(int s)
{
struct sockaddr_in address;
int size = sizeof(struct sockaddr_in);
char chargenbuff[CHARGENSIZE];
int length;
length = recvfrom(s, chargenbuff, CHARGENSIZE, 0, (struct sockaddr *)
&address, &size);
if (length > 0) {
for (length=0; length<72; ++length) {
chargenbuff[length] = 32+length;
}
chargenbuff[72] = '\r'; chargenbuff[73] = '\n';
sendto(s, chargenbuff, 74, 0, (struct sockaddr *)&address, size);
}
}
/*---- the background I/O handler function ----*/
void chargen_data(inet_handler *ih, worker_io_flags f)
{
char data[CHARGENSIZE];
int result;
int count, t;
UNUSED(f);
t = ih->data.chargen.state;
for (count=0; count<72; ++count) {
data[count] = t++;
if (t == 127) t=' ';
}
data[72] = '\r'; data[73] = '\n';
result = send(ih->data_socket, data+ih->data.chargen.offset,
74-ih->data.chargen.offset, 0);
if (result < 0) {
if (errno != EWOULDBLOCK) {
syslog(0,"chargen dying: %d %s", errno, socket_errno_to_string());
worker_deregister(ih->data_socket);
}
}
else {
ih->data.chargen.offset += result;
if (ih->data.chargen.offset >= 74) {
ih->data.chargen.offset = 0;
if (++ih->data.chargen.state == 127) {
ih->data.chargen.state = ' ';
}
}
}
}